package com.siwei.mes.tgl.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.siwei.mes.entity.Project;
import com.siwei.mes.entity.tgl.TglProject;
import com.siwei.mes.mapper.ProjectMapper;
import com.siwei.mes.mapper.TglProjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * tgl工程
 *
 * @Description: tgl工程
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/1/13 11:24
 */
@Service
public class SyncTglProjectService {
    private static final Logger log = LoggerFactory.getLogger("tgl");
    @Resource
    private TglProjectMapper tglProjectMapper;
    @Resource
    private ProjectMapper projectMapper;

    public void operateTglProject(TglProject entity) {
        log.info("TGL新增或修改工程,入参：{}", JSON.toJSONString(entity));
        /**第一步：根据tgl工程id判断是否存在*/
        TglProject localInfo = tglProjectMapper.selectByProId(entity.getProid());
        if (localInfo != null) {
            if (ObjectUtil.isEmpty(localInfo.getSynctime()) || entity.getSynctime() > localInfo.getSynctime()) {
                entity.setId(localInfo.getId());
                tglProjectMapper.updateByPrimaryKeySelective(entity);
            }
        } else {
            tglProjectMapper.insertSelective(entity);
        }
        /**第二步：修改本系统的工程信息是否存在*/
        Project addProject = new Project();
        addProject.setContractId(Long.valueOf(entity.getCid()));
        addProject.setTglId(String.valueOf(entity.getProid()));
        addProject.setThId(String.valueOf(entity.getThprojectid()));
        addProject.setProjectName(entity.getProjectname());
        addProject.setProjectAdd(entity.getProjectaddress());
        addProject.setLinkman(entity.getProjectlinkman());
        addProject.setTel(entity.getProjectlinktel());
        addProject.setLatitude(entity.getLat());
        addProject.setLongitude(entity.getLng());
        addProject.setExtra(entity.getExtrainfo());
        addProject.setAddTime(entity.getCreatettime());
        Project project = projectMapper.selectByTglId(String.valueOf(entity.getProid()));
        if (project != null) {
            addProject.setId(project.getId());
            projectMapper.updateByPrimaryKey(addProject);
        } else {
            projectMapper.insertSelective(addProject);
        }
    }
}
